package com.fw.application.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.TbAbout;
import com.fw.system.web.model.entity.TbAmountLog;
import com.fw.system.web.model.entity.TbUser;
import com.fw.system.web.service.ITbAboutService;
import com.fw.system.web.service.ITbAmountLogService;
import com.fw.system.web.service.ITbUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

import static com.fw.mes.ResultUtils.success;

/**
 * <p>
 * 平台介绍 前端控制器
 * </p>
 *
 * @author
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/about")
@Api(tags = "平臺介紹")
@CrossOrigin
public class TbAboutController {


    @Autowired
    private ITbAboutService aboutService;
    @Autowired
    private ITbAmountLogService amountLogService;
    @Autowired
    private ITbUserService userService;

    @GetMapping("/getAbout")
    @ApiOperation("得到平臺介紹")
    public Result getAbout() {

        return success(aboutService.getOne(Wrappers.<TbAbout>lambdaQuery().eq(TbAbout::getIsStatus, 1)));
    }

    @GetMapping("/getPhone")
    @ApiOperation("联系我们接口")
    public Result getPhone() {

        return success(aboutService.getOne(Wrappers.<TbAbout>lambdaQuery().eq(TbAbout::getIsStatus, 7)));
    }

    @GetMapping("/getMsg")
    @ApiOperation("平台通知")
    public Result getMsg() {

        return success(aboutService.getOne(Wrappers.<TbAbout>lambdaQuery().eq(TbAbout::getIsStatus, 6)));
    }

    @GetMapping("/getMsgList")
    @ApiOperation("获取谁充值了谁购买了消息前端分页查滚动播出")
    public Result getMsgList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        ArrayList<StringBuilder> list = Lists.newArrayList();
        amountLogService.list(Wrappers.<TbAmountLog>lambdaQuery().orderByDesc(TbAmountLog::getCreateTime)).forEach(item -> {
            DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:ss");
            String strDate3 = dtf3.format(item.getCreateTime());
            TbUser user = userService.getById(item.getUserId());
            StringBuilder stringBuilder = new StringBuilder();
            if (user != null) {
                if (!StringUtils.isBlank(user.getUserName())) {
                    if (StringUtils.isBlank(item.getLogName()))
                        item.setLogName("进行了充值");
                    stringBuilder.append(strDate3).append(user.getUserName()).append(item.getLogName());
                    list.add(stringBuilder);
                } else {
                    stringBuilder.append(strDate3).append("用户******").append(item.getLogName());
                    list.add(stringBuilder);
                }
            }


        });
        PageInfo pageInfo = new PageInfo(list);

        return success(pageInfo);
    }
}

